package com.xj;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author xujiang
 * @date 2021/1/31 17:41
 */
public class ElasticSearchClientTest {

    @Test
    public void createIndex() throws UnknownHostException {
//        1、创建一个Settings对象，相对于是一个配置信息。主要配置集群的名称。
        Settings settings = Settings.builder()
                .put("cluster.name", "my‐elasticsearch")
                .build();
//        2、创建一个客户端Client对象
        PreBuiltTransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9301));
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9302));
//        3、使用client对象创建一个索引库
        client.admin().indices().prepareCreate("index_hello")
                // 前面是准备工作，get是执行操作
                .get();

//        4、关闭client对象
        client.close();
    }

    @Test
    public void setMappings() throws IOException {
        Settings settings = Settings.builder()
                .put("cluster.name", "my‐elasticsearch")
                .build();
        PreBuiltTransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9301));
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9302));

        JSONObject jsonObject = JSON.parseObject("{\n" +
                "    \"hello\":{\n" +
                "        \"properties\":{\n" +
                "            \"id\":{\n" +
//                "                \"type\":\"long\",\n" +
                "                \"store\":true\n" +
                "            },\n" +
                "            \"title\":{\n" +
//                "                \"type\":\"text\",\n" +
                "                \"store\":true,\n" +
                "                \"index\":true,\n" +
                "                \"analyzer\":\"standard\"\n" +
                "            },\n" +
                "            \"content\":{\n" +
//                "                \"type\":\"text\",\n" +
                "                \"store\":true,\n" +
                "                \"index\":true,\n" +
                "                \"analyzer\":\"standard\"\n" +
                "            }\n" +
                "        }\n" +
                "    }\n" +
                "}", JSONObject.class);


        XContentBuilder builder = XContentFactory.jsonBuilder()
                .startObject()
                .startObject("article")
                .startObject("properties")
                .startObject("id")
                .field("type", "integer").field("store", "yes")
                .endObject()
                .startObject("title")
                .field("type", "string").field("store", "yes").field("analyzer", "ik_smart")
                .endObject()
                .startObject("content")
                .field("type", "string").field("store", "yes").field("analyzer", "ik_smart")
                .endObject()
                .endObject()
                .endObject()
                .endObject();
        System.out.println(jsonObject.toJSONString());
        client.admin().indices().preparePutMapping("index_hello")
                .setType("article")
                .setSource(builder).get();
        client.close();


    }
    @Test
//创建文档(通过XContentBuilder)
    public void test4() throws Exception{
        // 创建Client连接对象
        Settings settings = Settings.builder()
                .put("cluster.name", "my‐elasticsearch")
                .build();
        PreBuiltTransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9301));
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9302));

        //创建文档信息
        XContentBuilder builder = XContentFactory.jsonBuilder()
                .startObject()
                .field("id", 1)
                .field("title", "ElasticSearch是一个基于Lucene的搜索服务器")
                .field("content",
                        "它提供了一个分布式多用户能力的全文搜索引擎，基于RESTful web接口。Elasticsearch是用Java开发的，并作为Apache许可条款下的开放源码发布，是当前流行的企业级搜索引擎。设计用于云计算中，能够达到实时搜索，稳定，可靠，快速，安装使用方便。")
                .endObject();

        // 建立文档对象
        /**
         * 参数一blog1：表示索引对象
         * 参数二article：类型
         * 参数三1：建立id
         */
        client.prepareIndex("blog2", "article", "1").setSource(builder).get();

        //释放资源
        client.close();
    }

    @Test
//创建文档(通过实体转json)
    public void test5() throws Exception{
        // 创建Client连接对象
        Settings settings = Settings.builder()
                .put("cluster.name", "my‐elasticsearch")
                .build();
        PreBuiltTransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9301));
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9302));


        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", 2);
        jsonObject.put("title", "搜索工作其实很快乐");
        jsonObject.put("content", "我们希望我们的搜索解决方案要快，我们希望有一个零配置和一个完全免费的搜索模式，我们希望能够简单地使用JSON通过HTTP的索引数据，我们希望我们的搜索服务器始终可用，我们希望能够一台开始并扩展到数百，我们要实时搜索，我们要简单的多租户，我们希望建立一个云的解决方案。Elasticsearch旨在解决所有这些问题和更多的问题。");

        System.out.println(jsonObject.toJSONString());

        // 建立文档
        client.prepareIndex("blog2", "article", jsonObject.get("id").toString())
                //.setSource(objectMapper.writeValueAsString(article)).get();
                .setSource(jsonObject.toJSONString().getBytes(), XContentType.JSON).get();

        //释放资源
        client.close();
    }

}
